﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ASPNETDBTableAdapters;
using System.Data.SqlClient;
using System.Data;

public partial class Styles_Show : System.Web.UI.Page
{
    private ContentTableAdapter contentAdapter = new ContentTableAdapter();
    private NavigationTableAdapter tableAdapter = new NavigationTableAdapter();

    protected override void OnPreInit(EventArgs e)
    {
        if (Session["masterPage"] == null)
        {
            SettingsTableAdapter sAdapter = new SettingsTableAdapter();
            ASPNETDB.SettingsDataTable table = sAdapter.GetMasterPage();
            Session["masterPage"] = table.Rows[0]["masterPage"].ToString().Trim();
        }
        Page.MasterPageFile = String.Format("~/Styles/{0}.Master", Session["masterPage"]);
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        buildNavigation();
        showContent(Request.QueryString["contentID"]);
    }

    private void buildNavigation()
    {
        //Create the connection and DataAdapter for the Authors table.
        SqlConnection cnn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ASPNETDBConnectionString"].ConnectionString);
        SqlDataAdapter cmd1 = new SqlDataAdapter("select * from Navigation ORDER BY navigationPosition", cnn);

        //Create and fill the DataSet.
        DataSet ds = new DataSet();
        cmd1.Fill(ds, "navigations");

        //Create a second DataAdapter for the Titles table.
        SqlDataAdapter cmd2 = new SqlDataAdapter("select * from Content", cnn);
        cmd2.Fill(ds, "contents");

        //Create the relation bewtween the Authors and Titles tables.
        ds.Relations.Add("myrelation",
        ds.Tables["navigations"].Columns["navigationID"],
        ds.Tables["contents"].Columns["navigationID"]);

        //Bind the Authors table to the parent Repeater control, and call DataBind.
        NaviRepeater.DataSource = ds.Tables["navigations"];
        Page.DataBind();

        //Close the connection.
        cnn.Close();
    }

    private void showContent(String idParam)
    {
        if (idParam != null)
        {

            int id = int.Parse(idParam);
            ASPNETDB.ContentDataTable content = contentAdapter.GetContentById(id);
            if (content.Rows.Count != 0)
            {
                ASPNETDB.ContentRow item = (ASPNETDB.ContentRow)content.Rows[0];
                ContentTitle.InnerText = item["contentTitle"].ToString();
                ContentDate.Text = item["contentDate"].ToString();
                ContentText.InnerHtml = item["contentText"].ToString();
            }
        }
        else
        {
            ContentTitle.InnerHtml = "Welcome to the Website";
            ContentDate.Text = DateTime.Today.Date.ToString();
            ContentText.InnerHtml = "<br>Please feel free to use the navigation to browse all the magnificent content I created for you. So, let's begin the journey, use the force to select whatever you want!";
        }
    }
}